<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="./vue.js"></script>
    <link rel="stylesheet" href="./index.css" />
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet" />
</head>

<body>
    <div id="app">
        <div class="control">
            <input type="text" class="search" v-model="search" />
            <div>
                <label for="#all" v-for="item in radios">
                    {{item[0]}}:  <input id="all" type="radio" :value="item[1]" v-model="sex">
                </label>
            </div>
        </div>
        <ul>
            <li class="person" v-for="item in _getList(sex)">
                <div class="avatar">
                    <img :src="item.img" :alt="item.des" />
                </div>
                <div class="content">
                    <span class="name">{{item.name}} ({{item.sex=="m"? "男":"女"}})</span>
                    <span class="des">{{item.des}}</span>
                </div>
            </li>
        </ul>
        <nav aria-label="Page navigation">
            <ul class="pagination">
                <li v-for="(num,index) in 6" @click="click(index)" :key="num" :class="{active:clickindex===index}">
                    <a>{{num}}</a></li>
            </ul>
        </nav>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/Mock.js/1.0.1-beta3/mock-min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        function getPersonList(num) {
            return new Array(num).fill(0).map(() =>
                Mock.mock({
                    name: Mock.Random.cname(),
                    img: Mock.Random.image("100x100", Mock.Random.color()),
                    des: Mock.Random.csentence(10, 30),
                    "sex|1": ["m", "f"],
                    id: Mock.Random.id(),
                })
            );
        }

        Mock.mock("/user", () => getPersonList(10));
        var vm = new Vue({
            el: "#app",
            data: {
                sex: "x",
                radios: [
                    ['男', 'm'],
                    ['女', 'f'],
                    ['全部', 'x']
                ],
                userList: [],
                AllList: [],
                clickindex: 0,
                search: "",
            },
            methods: {
                click(index = 0) {
                    console.log("click")
                    this.clickindex = index,
                        this._getUser()
                },
                  _getList(choice,AllList){
                    AllList=this.userList;
                    var mList=AllList.filter((item)=>item.sex==="m")
                    var fList=AllList.filter((item)=>item.sex==="f")
                    var obj={"x":AllList,"m":mList,"f":fList,}
                    return obj[choice];
                  },
                _getUser() {
                    var that = this
                    $.ajax({
                        url: "/user",
                        success: function (data) {
                            that.AllList = [];
                            that.search = ""
                            console.log(data)
                            that.userList = JSON.parse(data)
                        }
                    })
                }

            },
            created() {
                this._getUser()
            }
        })
    </script>
</body>

</html>